using System.Collections.Generic;
using CfgTable;
using IQIGame.Onigao.Logic;
using NetProtocol.Enum;
using NetProtocol.POD;

namespace IQIGame.Onigao.GamePlay
{
    public partial class FakeDataCenter
    {
        public long battleId { get; set; } = 1;
        public bool isLocalBattle { get; set; } = true;

        public CreateBattlePOD CreateBattlePOD()
        {
            var rHeros = new[] { 100101, 100201, 100301, 100401 }; //角色ID
            var rMonsterTeam = new[] { 20514001, 20514001 }; //队伍ID
            var rPets = new[] { 10001, 0, 0, 0 };
            var rBattleSceneConfig = TableCenter.battleScene.Get(TableCenter.monsterTeam.Get(rMonsterTeam[0]).BattleScene);
            return new CreateBattlePOD
            {
                id = battleId,
                randomSeed = 1,
                battleScene = rBattleSceneConfig.Id,
                hostPlayerId = playerId,
                isLocal = isLocalBattle,
                strikeTeam = BattleTeamId.Null, // 突袭 -1正常0我方1敌方
                unsafe_redTeams = FakePlayerTeam(rHeros, rPets),
                unsafe_blueTeams = FakeMonsterTeam(rMonsterTeam, rBattleSceneConfig),
                unsafe_allPlayers = new List<long> { playerId },
            };
        }

        private List<BattleTeamPOD> FakePlayerTeam(int[] heros, int[] pets)
        {
            var rList = new List<BattleUnitPOD>();
            var rPOD = new BattleTeamPOD
            {
                teamIndex = BattleConst.TeamRedIndex,
                unsafe_units = rList,
            };
            for (int i = 0; i < heros.Length; i++)
            {
                var rConfig = TableCenter.hero.Get(heros[i]);
                var rAttrType = new List<int>(rConfig.AttType);
                var rAttrValue = new List<int>(rConfig.AttValue);
                var rSkill = new List<int>(rConfig.DefaultSkill);
                var rSkillDict = new Dictionary<int, int>();
                for (int j = 0; j < rSkill.Count; j++)
                {
                    rSkillDict.Add(rSkill[j], 1);
                }
                var rAttrArr = new int[(int)EAttributeType.Max];
                for (int j = 0; j < rAttrType.Count; j++)
                {
                    rAttrArr[rAttrType[j]] = rAttrValue[j];
                }
                rAttrArr[(int)EAttributeType.Hp] = rAttrArr[(int)EAttributeType.MaxHp];
                rAttrArr[(int)EAttributeType.SP] = rAttrArr[(int)EAttributeType.InitSP];
                var rUnitPOD = new BattleUnitPOD
                {
                    configID = heros[i],
                    pid = playerId,
                    skinID = 0,
                    attrs = rAttrArr,
                    unsafe_initBuff = new List<int>(),
                    unsafe_skills = rSkillDict,
                    formationIndex = i,
                    level = 10,
                    unitType = (int)EUnitType.Hero,
                    radius = rConfig.Radius,
                    ai = rConfig.AI,
                    pet = pets.Get(i)
                };
                rList.Add(rUnitPOD);
            }
            // return rPOD;
            {
                int i = 1;
                var rConfig = TableCenter.hero.Get(heros[i]);
                var rAttrType = new List<int>(rConfig.AttType);
                var rAttrValue = new List<int>(rConfig.AttValue);
                var rSkill = new List<int>(rConfig.DefaultSkill);
                var rSkillDict = new Dictionary<int, int>();
                for (int j = 0; j < rSkill.Count; j++)
                {
                    rSkillDict.Add(rSkill[j], 1);
                }
                var rAttrArr = new int[(int)EAttributeType.Max];
                for (int j = 0; j < rAttrType.Count; j++)
                {
                    rAttrArr[rAttrType[j]] = rAttrValue[j];
                }
                rAttrArr[(int)EAttributeType.Hp] = rAttrArr[(int)EAttributeType.MaxHp];
                rAttrArr[(int)EAttributeType.SP] = rAttrArr[(int)EAttributeType.InitSP];
                var rUnitPOD = new BattleUnitPOD
                {
                    configID = heros[i],
                    pid = playerId,
                    skinID = 0,
                    attrs = rAttrArr,
                    unsafe_initBuff = new List<int>(),
                    unsafe_skills = rSkillDict,
                    formationIndex = i,
                    level = 10,
                    unitType = (int)EUnitType.Hero,
                    radius = rConfig.Radius,
                    ai = rConfig.AI
                };
                rPOD.substitute = rUnitPOD;
            }
            return new List<BattleTeamPOD>() { rPOD };
        }

        private List<BattleTeamPOD> FakeMonsterTeam(int[] rMonsterTeam, CfgBattleScene rSceneConfig)
        {
            var rListTeam = new List<BattleTeamPOD>();
            for (int i = 0; i < rMonsterTeam.Length; i++)
            {
                var rList = new List<BattleUnitPOD>();
                var rPOD = new BattleTeamPOD
                {
                    teamIndex = BattleConst.TeamBlueIndex,
                    unsafe_units = rList,
                };
                rListTeam.Add(rPOD);
                var rMonsterTeamConfig = TableCenter.monsterTeam.Get(rMonsterTeam[i]);
                for (int j = 0; j < rMonsterTeamConfig.MonsterId.Count; j++)
                {
                    if (rMonsterTeamConfig.MonsterId[j] == 0)
                    {
                        continue;
                    }
                    var rAttrType = new List<int>();
                    var rAttrValue = new List<int>();

                    var nMonsterID = rMonsterTeamConfig.MonsterId[j];
                    var rConfig = TableCenter.monster.Get(nMonsterID);
                    var nPower = rMonsterTeamConfig.MonsterPower[j];
                    var nPowerID = rMonsterTeamConfig.MonsterPowerType[j];
                    var rMonsterCorr = rMonsterTeamConfig.MonsterAttrCorr[j];
                    UnitFactory.GetMonsterPanelAttr(nPower, nPowerID, rMonsterCorr, ref rAttrType, ref rAttrValue, out int nWeakNum);

                    var rSkill = new List<int>(rConfig.Skill);
                    var rSkillDict = new Dictionary<int, int>();
                    for (int k = 0; k < rSkill.Count; k++)
                    {
                        rSkillDict.Add(rSkill[k], 1); // 默认1级
                    }
                    var rAttrArr = new int[(int)EAttributeType.Max];
                    for (int k = 0; k < rAttrType.Count; k++)
                    {
                        rAttrArr[rAttrType[k]] = rAttrValue[k];
                    }
                    rAttrArr[(int)EAttributeType.Hp] = rAttrArr[(int)EAttributeType.MaxHp];
                    rAttrArr[(int)EAttributeType.SP] = rAttrArr[(int)EAttributeType.InitSP];
                    var rUnitPOD = new BattleUnitPOD
                    {
                        configID = nMonsterID,
                        pid = playerId,
                        attrs = rAttrArr,
                        unsafe_skills = rSkillDict,
                        unsafe_initBuff = new List<int>(),
                        formationIndex = j,
                        level = 10,
                        weaknessPoint = nWeakNum,
                        unitType = (int)EUnitType.Monster,
                        radius = rConfig.Radius,
                        ai = rConfig.AI
                    };
                    // 转二进制
                    for (int k = 0; k < rConfig.WeakType.Count; k++)
                    {
                        rUnitPOD.weaknessType |= 1 << rConfig.WeakType[k];
                    }
                    rList.Add(rUnitPOD);
                }
            }
            return rListTeam;
        }
    }
}